package face.pay.sharing.biz;

import com.alibaba.fastjson.JSON;
import com.face.pay.WxApiConfig;
import com.github.wxpay.sdk.WXPayConstants;
import com.github.wxpay.sdk.WXPayRequest;
import com.github.wxpay.sdk.WXPayUtil;
import face.pay.dao.SharingAccountDao;
import face.pay.entity.SharingMchAccount;
import face.pay.vo.VSharingAccount;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
 * 添加纷争对象
 */
@Slf4j
@Component
public class AddSharingAccountBiz {

    private final static   String domain    = "api.mch.weixin.qq.com";

    private final static   String urlSuffix = "/pay/profitsharingaddreceiver";


    @Autowired
    SharingAccountDao sharingAccountDao;

    @Transactional(rollbackFor = Exception.class)
    public Boolean addAccount(WxApiConfig wxApiConfig, String subMchId, VSharingAccount vSharingAccount){

        Boolean success = false;
        try{

            SharingMchAccount sharingMchAccount = new SharingMchAccount();


            WXPayRequest wxPayRequest = new WXPayRequest(wxApiConfig);

            String appId = wxApiConfig.getAppID();
            String apiKey = wxApiConfig.getKey();
            String mchId = wxApiConfig.getMchID();

            String nonce_str = WXPayUtil.generateNonceStr();

            Map<String, String> map = new HashMap<>();
            map.put("mch_id",mchId);
            map.put("nonce_str",nonce_str);
            map.put("sub_mch_id",subMchId);
            map.put("appid",appId);

            Map<String,Object> receiver =  new HashMap<>();
            receiver.put("type",vSharingAccount.getAccountType());
            receiver.put("account",vSharingAccount.getAccount());
            receiver.put("name",vSharingAccount.getAccountName());

            receiver.put("relation_type",vSharingAccount.getRelationType());
            String receiversStr  = JSON.toJSONString(receiver);
            map.put("receiver",receiversStr);

            String xml = WXPayUtil.generateSignedXml(map, apiKey, WXPayConstants.SignType.HMACSHA256);

            log.info("刷脸支付 推送到微信服务器消息xml" + xml);
            String result = wxPayRequest.requestOnce(domain, urlSuffix, xml, false);
            log.info("返回结果 " + result);
            Map<String, String> resultMap = WXPayUtil.xmlToMap(result);
            Boolean isSuccess =   WXPayUtil.isSuccess(resultMap);
            if(isSuccess){
                /**
                 * 添加分账成功
                 */
                sharingMchAccount.setStatus(1);
                vSharingAccount.setStatus(1);
                success = true;
            }else {

                String  err_code    = resultMap.get("err_code");
                String  err_code_des= resultMap.get("err_code_des");

                sharingMchAccount.setMessage(err_code_des);
                sharingMchAccount.setStatus(0);
                vSharingAccount.setStatus(0);
                vSharingAccount.setMessage(err_code_des);


            }
            sharingMchAccount.setId(vSharingAccount.getId());

            sharingAccountDao.update(sharingMchAccount);
        }catch (Exception e){
            log.error("添加分账对象异常",e);
        }

        return success;

    }
}
